﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using KM.BUS;
using KM.ENT;
using CORE;
using KM.Models;

namespace KM.Areas.REM.Controllers.News
{
    public class NewsController : Controller
    {
        //
        // GET: /REM/News/
        int pageSize = 10;
        [ChildActionOnly]
        public PartialViewResult DanhSachNewFooter(int Id)
        {
            busNews busN = new busNews();
            List<entNews> lst = new List<entNews>();
            lst = busN.getAll(Id, Constans.SOTINHIENTHI);
            return PartialView(lst);
        }
        public ActionResult DanhSachNew(int Id, string Name)
        {
            busNews busSP = new busNews();
            List<entNews> lst = new List<entNews>();
            PagingInfoModel page = new PagingInfoModel()
            {
                CurrentPage = Utils.getIntQuery("page") == 0 ? 1 : Utils.getIntQuery("page"),
                recordPerPage = pageSize
            };

            //kiem tra neu la trang hien tai=1, so luong tin =1 thi chuyen ve trang chi tiet
            
            lst = busSP.getAll(Id, ref page);
            if (lst.Count == 1 && page.CurrentPage == 1)
            {
                return RedirectToAction("ChiTiet", null, new { Id = lst[0].Id, Name=Utils.GetTitle(lst[0].Name) });
            }
            ViewBag.page = page;
            ViewBag.Id = Id;
            ViewBag.Name = Name;
            busGroupNews busLT = new busGroupNews();
            entGroupNews entLT = new entGroupNews();
            entLT = busLT.getInfo(Id);

            busConfig busCF = new busConfig();
            entConfig entCF = new entConfig();
            entCF = busCF.getInfo(1);
            if (entLT.Title == "")
            {
                ViewBag.Title = entCF.Title;
                ViewBag.keywords = entCF.Keyword;
                ViewBag.description = entCF.Description;
            }
            else
            {
                ViewBag.Title = entLT.Title;
                ViewBag.keywords = entLT.Keyword;
                ViewBag.description = entLT.Description;

            }
            return View(lst);
        }
        public ActionResult ChiTiet(int Id)
        {
            entNews entN = new entNews();
            busNews busN = new busNews();
            entN = busN.getInfo(Id);

            busConfig busCF = new busConfig();
            entConfig entCF = new entConfig();
            entCF = busCF.getInfo(1);
            if (entN.Title == "")
            {
                ViewBag.Title = entCF.Title;
                ViewBag.keywords = entCF.Keyword;
                ViewBag.description = entCF.Description;
            }
            else
            {
                ViewBag.Title = entN.Title;
                ViewBag.keywords = entN.Keyword;
                ViewBag.description = entN.Description;

            }
            return View(entN);
        }
        public ActionResult GioiThieu()
        {
            entNews entN = new entNews();
            busNews busN = new busNews();
            entN = busN.getInfo(10);
            busConfig busCF = new busConfig();
            entConfig entCF = new entConfig();
            entCF = busCF.getInfo(1);
            if (entN.Title == "")
            {
                ViewBag.Title = entCF.Title;
                ViewBag.keywords = entCF.Keyword;
                ViewBag.description = entCF.Description;
            }
            else
            {
                ViewBag.Title = entN.Title;
                ViewBag.keywords = entN.Keyword;
                ViewBag.description = entN.Description;

            }
            return View(entN);
        }
        public ActionResult LienHe()
        {
            entNews entN = new entNews();
            busNews busN = new busNews();
            entN = busN.getInfo(12);
            NewsLienHeModel newsLH = new NewsLienHeModel();
            newsLH.entN = entN;
            busConfig busCF = new busConfig();
            entConfig entCF = new entConfig();
            entCF = busCF.getInfo(1);
            if (entN.Title == "")
            {
                ViewBag.Title = entCF.Title;
                ViewBag.keywords = entCF.Keyword;
                ViewBag.description = entCF.Description;
            }
            else
            {
                ViewBag.Title = entN.Title;
                ViewBag.keywords = entN.Keyword;
                ViewBag.description = entN.Description;

            }
            return View(newsLH);
        }
        [HttpPost]
        public ActionResult LienHe(NewsLienHeModel newsLH)
        {
            if (!ModelState.IsValid)
            {
                //lay lai gia trị cua tin lien hệ vì các nhãn ở dạng lable nen không binding dc
                entNews entN = new entNews();
                busNews busN = new busNews();
                entN = busN.getInfo(12);
                newsLH.entN = entN;
                return View(newsLH);
            }
            busLienHe busLH = new busLienHe();
            busLH.createEdit(newsLH.entLH);
            Session["me"] = Constans.GUILHTC;
            return RedirectToAction("LienHe");
        }

    }
}
